package com.hqyj.smvc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hqyj.smvc.mapper.EmployeeMapper;
import com.hqyj.smvc.mapper.RoleMapper;
import com.hqyj.smvc.pojo.Employee;
import com.hqyj.smvc.pojo.Role;
import com.hqyj.smvc.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    @Qualifier("roleMapper")
    private RoleMapper roleMapper;
    @Autowired
    @Qualifier("employeeMapper")
    private EmployeeMapper employeeMapper;

    /**
     * 查询角色信息
     * @return 返回角色集合
     */
    @Override
    public List<Role> allRole(int num){
        Page<Role> rolePage = PageHelper.startPage(num, 3);
        List<Role> roles = roleMapper.selectAllRole();
        return roles;
    }

    /**
     * 查看角色的详细信息
     * @param id
     * @return
     */
    @Override
    public List<Employee> roleDetailedInformation(int id){
        List<Employee> employees = employeeMapper.roleIdEmployee(id);
        return employees;
    }

    /**
     * 添加角色方法，同时选择雇员
     * @param role_name 新角色名称
     * @param str 雇员id
     */
    @Override
    public boolean appendRole(String role_name, String str){
        boolean boo = false;
        Role role = new Role();
        if(!"".equals(role_name)){
            role.setRole_name(role_name);
            int insertRole = roleMapper.insertRole(role);
            if (insertRole > 0){
                boo = true;
            }
        }
        Integer role_id = role.getRole_id();
        if (!"".equals(str) && role_id > 0){
            String[] split = str.split(",");
            for (String st:split) {
                int employee_id = Integer.parseInt(st);
                Map<String, Integer> map = new HashMap();
                map.put("role_id", role_id);
                map.put("employee_id", employee_id);
                int employee = roleMapper.insertRoleEmployee(map);
            }
        }
        return boo;
    }



}
